package com.yunji.news.controller;

import com.yunji.news.common.Result;
import com.yunji.news.entity.Category;
import com.yunji.news.mapper.CategoryMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 分类控制器
 *
 * @author yunji
 * @since 2024-11-23
 */
@RestController
@RequestMapping("/categories")
@CrossOrigin
public class CategoryController {
    
    @Autowired
    private CategoryMapper categoryMapper;
    
    /**
     * 获取所有启用的分类
     */
    @GetMapping
    public Result<List<Category>> getCategories() {
        List<Category> categories = categoryMapper.selectEnabledCategories();
        return Result.success(categories);
    }
    
    /**
     * 根据编码获取分类
     */
    @GetMapping("/code/{code}")
    public Result<Category> getCategoryByCode(@PathVariable String code) {
        Category category = categoryMapper.selectByCode(code);
        if (category == null) {
            return Result.error("分类不存在");
        }
        return Result.success(category);
    }
}